package com.maxd.modules.coupon.mapper;

import com.maxd.modules.coupon.entity.CouponAmount;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Auther: Laban:yubo_010@163.com
 * @Date: 2020/11/13 14:25
 * @Description:
 */
@Repository
public interface CouponAmountMapper extends JpaRepository<CouponAmount, Long> {

    /**
     * 根据id修改
     */
    @Transactional
    @Modifying
    @Query(nativeQuery = true,value = "update sqx_coupon_amount set user_deduct_quota=:#{#couponAmount.userDeductQuota}," +
            "user_province_amount=:#{#couponAmount.userProvinceAmount},user_frozen_amount=:#{#couponAmount.userFrozenAmount}," +
            "user_id=:#{#couponAmount.userId} where id=:#{#couponAmount.id}")
    void updateCouponAmountById(@Param("couponAmount")CouponAmount couponAmount);

    /**
     * 根据UserId查询
     */
    @Query(nativeQuery = true,value = "SELECT * FROM sqx_coupon_amount WHERE user_id=:userId")
    CouponAmount getCouponAmountByUserId(@Param("userId") String userId);


    /**
     * 根据userid修改
     */
    @Transactional
    @Modifying
    @Query(nativeQuery = true,value = "update sqx_coupon_amount set user_deduct_quota=:#{#couponAmount.userDeductQuota}," +
            "user_province_amount=:#{#couponAmount.userProvinceAmount},user_frozen_amount=:#{#couponAmount.userFrozenAmount} " +
            "where user_id=:#{#couponAmount.userId}")
    void updateCouponAmountByUserId(@Param("couponAmount")CouponAmount couponAmount);

}
